import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import {getArticle} from '@/services'

export interface IndexModelState {
    list:Array<any>
}

export interface IndexModelType {
    namespace: 'article';
    state: IndexModelState;
    effects: {
        getlist: Effect;
    };
    reducers: {
        save: Reducer<IndexModelState>;
        // 启用 immer 之后
        // save: ImmerReducer<IndexModelState>;
    };
    subscriptions?: { setup: Subscription };
}



const IndexModel: IndexModelType = {
    // namespace:命名空间
    namespace: 'article',
    // state:redux中的存储的状态,
    state: {
        list: [],
    },
    // effects:异步action
    effects: {
        *getlist({ payload }, { call, put }) {
            let params ={
                page:payload.page,
                pageSize:payload.pageSize
            }
            
            // 把接口的函数放在 call 里面就好,然后后面跟传的参数
            let result = yield call(getArticle,params); //接口
            console.log(result,"1111111111111111");
            
            // 有结果以后 还想 发起请求 就 put   
            yield put({ // put相当于dispatch
                type:'save',
                payload:{
                    list:result.data
                }
            })

         },
    },
    // reducers: 同步action
    reducers: {
        save(state, action) {
            return {
                ...state,
                ...action.payload,
            };
        },
    },
};

export default IndexModel;
